package com.party.web.biz.channel;

import com.party.common.utils.StringUtils;
import com.party.core.model.article.Article;
import com.party.core.model.channel.Channel;
import com.party.core.service.article.IArticleService;
import com.party.core.service.channel.IChannelService;
import com.party.core.service.cos.CosBizService;
import com.party.web.biz.article.ArticleBizService;
import com.party.web.biz.file.FileBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 频道删除业务处理
 *
 * @author yifeng
 * @date 2018/7/16 0016
 * @time 上午 11:07
 */
@Service
public class ChannelDeleteBizService {
    @Autowired
    private IChannelService channelService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private FileBizService fileBizService;
    @Autowired
    private CosBizService cosBizService;
    @Autowired
    private ArticleBizService articleBizService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 删除频道
     *
     * @param channelId 频道编号
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean deleteChannelBiz(String channelId) {
        boolean endResult = true;
        String memberId = null;
        try {
            if (StringUtils.isEmpty(channelId)) {
                endResult = false;
                return endResult;
            }
            Channel channel = channelService.get(channelId);
            if (channel == null) {
                endResult = false;
                return endResult;
            }
            memberId = channel.getMember();
            List<Article> articles = articleService.getByChannelId(channelId);
            for (Article article : articles) {
                boolean result = articleBizService.deleteArticleBiz(article.getId());
                if (!result) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return false;
                }
            }
            channelService.delete(channelId);
        } catch (Exception e) {
            endResult = false;
            logger.error("删除频道失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            if (endResult) {
                try {
                    String filePath = fileBizService.getUploadCIPath(memberId, "channel");
                    String detailPath = CosBizService.getDeleteQrCodePath(filePath, channelId);
                    cosBizService.deleteFile(detailPath);
                } catch (Exception e) {
                    logger.error("删除频道二维码失败", e);
                }
            }
        }
        return endResult;
    }
}
